//
//  RunViewController.m
//  DayDayRun
//
//  Created by 卫建 on 16/1/6.
//  Copyright © 2016年 卫建. All rights reserved.
//

#import "RunViewController.h"
#import "KindViewController.h"
#import "CNPGridMenu.h"
#import "WeatherViewController.h"
#import "WZMainViewController.h"
#import "SpeedViewController.h"
#import "CountDownViewController.h"

#import "KindViewController.h"


#import <AVFoundation/AVFoundation.h>
@interface RunViewController ()<CNPGridMenuDelegate>

@property (strong, nonatomic) IBOutlet UIButton *startupButton;
@property (strong, nonatomic) IBOutlet UIButton *weatherButton;
@property (strong, nonatomic) IBOutlet UIButton *GPSButton;
@property (strong, nonatomic) IBOutlet UIButton *titleButton;
@property (strong, nonatomic) IBOutlet UILabel *totalMailsLabel;
@property (strong, nonatomic) IBOutlet UILabel *totalTimeLabel;
@property (strong, nonatomic) IBOutlet UILabel *totalNumberLabel;



@property(nonatomic,strong)CNPGridMenuItem *speech;
@property(nonatomic,strong)CNPGridMenuItem *automaSuspend;
@property(nonatomic,strong)CNPGridMenuItem *nightMode;
@property(nonatomic,strong)CNPGridMenuItem *addMusic;
@property(nonatomic,strong)CNPGridMenuItem *automaticLock;
@property(nonatomic,strong)CNPGridMenuItem *tranPlan;
@property(nonatomic,strong)CNPGridMenuItem *moveTarget;
@property(nonatomic,strong)CNPGridMenuItem *mapSet;
@property(nonatomic,strong)CNPGridMenuItem *pickDate;


@end

@implementation RunViewController




#warning asdfghjkl
- (CMMotionManager *)motionManager
{
    CMMotionManager *motionManager = nil;
    
    id appDelegate = [UIApplication sharedApplication].delegate;
    if([appDelegate respondsToSelector:@selector(motionManager)])
        motionManager = [appDelegate motionManager];
    return motionManager;
}








- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //配置button
    [self setButton];
    
    //添加左侧的Item
    [self addLeftItem];
    
    //添加右侧的Item
    [self addRightItem];
    
    
    
}


-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
    
}


#pragma mark------配置界面上的button方法
-(void)setButton
{
    
    //将button搞成圆角
    self.titleButton.clipsToBounds = YES;
    self.titleButton.layer.cornerRadius = 20;
    [self.titleButton.layer setBorderWidth:1.0];
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef color = CGColorCreate(colorSpace,(CGFloat[]){1,1,1,1});
    [self.titleButton.layer setBorderColor:color];
    
    self.startupButton.clipsToBounds = YES;
    self.startupButton.layer.cornerRadius = 50;
    [self.startupButton.layer setBorderWidth:1.0];
    CGColorSpaceRef startupColorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef startupColor = CGColorCreate(startupColorSpace, (CGFloat[]){1,1,1,1});
    [self.startupButton.layer setBorderColor:startupColor];
    
    self.weatherButton.clipsToBounds = YES;
    self.weatherButton.layer.cornerRadius = 30;
    [self.weatherButton.layer setBorderWidth:0.8];
    CGColorSpaceRef weatherColorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef weatherColor = CGColorCreate(weatherColorSpace, (CGFloat[]){1,1,1,1});
    [self.startupButton.layer setBorderColor:weatherColor];
    
    
    self.GPSButton.clipsToBounds = YES;
    self.GPSButton.layer.cornerRadius = 30;
    [self.GPSButton.layer setBorderWidth:0.8];
    CGColorSpaceRef GPSColorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef GPSColor = CGColorCreate(GPSColorSpace, (CGFloat[]){1,1,1,1});
    [self.startupButton.layer setBorderColor:GPSColor];
    
    
    [self startDrifting];//开始执行,记录步数方法
    
}


#pragma mark------添加左侧Item的方法
-(void)addLeftItem
{
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"toshop_lightup_button"] style:UIBarButtonItemStyleDone target:self action:@selector(leftItemAction:)];
    self.navigationItem.leftBarButtonItem = leftItem;
}

#pragma mark-----添加右侧Item的方法
-(void)addRightItem
{
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"more_icon_channelmanage"] style:UIBarButtonItemStyleDone target:self action:@selector(rightItemAction:)];
    self.navigationItem.rightBarButtonItem = rightItem;
}

#pragma mark------实现leftItem的方法
-(void)leftItemAction:(UIBarButtonItem *)sender
{
    KindViewController *kindVC = [[KindViewController alloc]init];
    UINavigationController *kindNC = [[UINavigationController alloc]initWithRootViewController:kindVC];
    [self presentViewController:kindNC animated:NO completion:^{
        
    }];
    
    //传值一块
    [kindVC returnText:^(NSString *showText) {
        
        self.totalMailsLabel.text = [NSString stringWithFormat:@"%@总里程",showText];
    }];
}

#pragma mark--------实现rightItem的方法
-(void)rightItemAction:(UIBarButtonItem *)sender
{
    self.speech = [[CNPGridMenuItem alloc] init];
    self.speech.icon = [UIImage imageNamed:@"LaterToday"];
    self.speech.title = @"语音播报";
    
    self.automaSuspend = [[CNPGridMenuItem alloc] init];
    self.automaSuspend.icon = [UIImage imageNamed:@"ThisEvening"];
    self.automaSuspend.title = @"自动暂停";
    
    self.nightMode = [[CNPGridMenuItem alloc] init];
    self.nightMode.icon = [UIImage imageNamed:@"Tomorrow"];
    self.nightMode.title = @"夜间模式";
    
    self.automaticLock = [[CNPGridMenuItem alloc] init];
    self.automaticLock.icon = [UIImage imageNamed:@"ThisWeekend"];
    self.automaticLock.title = @"自动锁定";
    
    self.addMusic = [[CNPGridMenuItem alloc] init];
    self.addMusic.icon = [UIImage imageNamed:@"NextWeek"];
    self.addMusic.title = @"添加音乐";
    
    self.tranPlan = [[CNPGridMenuItem alloc] init];
    self.tranPlan.icon = [UIImage imageNamed:@"InMonth"];
    self.tranPlan.title = @"训练计划";
    
    self.moveTarget = [[CNPGridMenuItem alloc] init];
    self.moveTarget.icon = [UIImage imageNamed:@"Someday"];
    self.moveTarget.title = @"运动目标";
    
    self.mapSet = [[CNPGridMenuItem alloc] init];
    self.mapSet.icon = [UIImage imageNamed:@"Desktop"];
    self.mapSet.title = @"地图设置";
    
    self.pickDate = [[CNPGridMenuItem alloc] init];
    self.pickDate.icon = [UIImage imageNamed:@"PickDate"];
    self.pickDate.title = @"Pick Date";
    
    CNPGridMenu *gridMenu = [[CNPGridMenu alloc] initWithMenuItems:@[self.speech, self.automaSuspend, self.nightMode, self.automaticLock, self.addMusic, self.tranPlan, self.moveTarget, self.mapSet, self.pickDate]];
    gridMenu.delegate = self;
    [self presentGridMenu:gridMenu animated:YES completion:^{
        NSLog(@"Grid Menu Presented");
    }];

}

#pragma mark--------点击开始响应的方法
- (IBAction)startupButtonDidClicked:(id)sender
{
    CountDownViewController *countVC = [[CountDownViewController alloc]init];
    
    [self presentViewController:countVC animated:YES completion:^{
        
    }];
}

#pragma mark--------点击天气状况的button响应的事件
- (IBAction)wearherButtonDidClicked:(id)sender
{
    
    
    WZMainViewController *mainVC = [[WZMainViewController alloc]init];
    UINavigationController *naviVC = [[UINavigationController alloc]initWithRootViewController:mainVC];
    [self presentViewController:naviVC animated:YES completion:^{
        
    }];
    
}

#pragma mark------点击全员加速中响应的事件
- (IBAction)allPeopleFullSpeed:(id)sender
{
    SpeedViewController *speedVC = [[SpeedViewController alloc]init];
    UINavigationController *speedNC = [[UINavigationController alloc]initWithRootViewController:speedVC];
    [self presentViewController:speedNC animated:YES completion:^{
        
    }];
}


- (void)gridMenuDidTapOnBackground:(CNPGridMenu *)menu
{
    [self dismissGridMenuAnimated:YES completion:^{
        NSLog(@"Grid Menu Dismissed With Background Tap");
    }];
}

- (void)gridMenu:(CNPGridMenu *)menu didTapOnItem:(CNPGridMenuItem *)item
{
//    [self dismissGridMenuAnimated:YES completion:^{
//        NSLog(@"Grid Menu Did Tap On Item: %@", item.title);
//    }];
    
    if (item == self.speech)
    {
        NSString *warnmsg = @"daydayrun今天天气不错，出去耍耍怎么样?";
        NSString *m_strLang = @"";
        
        AVSpeechSynthesizer* player=[[AVSpeechSynthesizer alloc]init];
        AVSpeechUtterance* u=[[AVSpeechUtterance alloc]initWithString:m_strLang];//设置要朗读的字符串
        u.voice=[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];//设置语言
        
        
        
        if( [[[UIDevice currentDevice] systemVersion] integerValue] >= 7.0)
        {
            AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:warnmsg];
            utterance.rate *= 0.8;
            
            AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:[NSString stringWithFormat:@"%@",u]];
           
            //获取当前系统语音
            NSString *preferredLang = @"";
            if ([m_strLang  isEqual: @"zh-Hans"])
            {
                preferredLang = @"zh-CN";
            }
            else
            {
                preferredLang = @"en-US";
            }
            
            
            utterance.voice = voice;
            [player speakUtterance:utterance];
        }
        
        [self dismissGridMenuAnimated:YES completion:^{
            NSLog(@"Grid Menu Did Tap On Item: %@", item.title);
        }];
    }
    else if (item == self.automaSuspend)
    {
        [self dismissGridMenuAnimated:YES completion:^{
            NSLog(@"Grid Menu Did Tap On Item: %@", item.title);
        }];
    }
}



//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
//{
//    KindViewController *kindVC = segue.destinationViewController;
//    
//    [kindVC returnText:^(NSString *showText) {
//        self.totalMailsLabel.text = showText;
//    }];
//    
//    NSLog(@"%@",self.totalMailsLabel.text);
//}



#warning asdfghjkl
- (void)startDrifting
{
    [self.motionManager setAccelerometerUpdateInterval:15/60.f];
    //    UILabel *stepsTexLable = (UILabel *)[self.view viewWithTag:stepsTexLable_tag];
    [self.motionManager startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc] init] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
        dispatch_sync(dispatch_get_main_queue(), ^(void) {
            
            if (sqrt(accelerometerData.acceleration.x*accelerometerData.acceleration.x+accelerometerData.acceleration.y*accelerometerData.acceleration.y+accelerometerData.acceleration.z*accelerometerData.acceleration.z)>1.052188)
            {
                //                NSLog(@"灵敏度: %f",self.scale);
                //                1.990037
//                _runNum += 1;
//                self.runNumberLable.text = [NSString stringWithFormat:@"%ld ",_runNum];
//                
//                NSLog(@"-------------------------------------------------------------------%@",self.runNumberLable.text);
//                
//                NSString *runNum = [NSString stringWithFormat:@"%ld",_runNum];//随时向用户中心存储步数
//                                [Defaults setObject:runNum forKey:@"runNum"];
//                
//                发布广播  当前步数
//                [[NSNotificationCenter defaultCenter]postNotificationName:@"RunNum" object:runNum];
                
            }
        });
    }];
}




- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}



@end
